package com.best.oasis.settlement.common.entity.sysLog;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Lob;
import javax.persistence.Table;

import org.springframework.security.Authentication;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.context.SecurityContextHolder;

import com.best.oasis.settlement.common.entity.base.BaseEntity;
import com.best.oasis.settlement.util.SecurityUtil;


/**
 * 系统关键操作日志
 * @author BL00064
 *
 */
@Entity
@Table(name="ST_SYSLOG")
//@SQLInsert( sql="INSERT INTO ST_SYSLOG(id, operationTime, operator, operatorIP, type, userPrivileges, description) VALUES(?,?,?,?,?,?,?)")
public class SysLog extends BaseEntity {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
    /**
     * 操作描述
     */
    @Lob
    private String zDescription;
    
    /**
     * 操作时间
     */
    private Date operationTime;
    /**
     * 操作人username
     */
    @Column(length=50,name="operator")
    private String operator;
    /**
     * 操作人权限列表
     */
    @Column(length=4000,name="userPrivileges")
    private String userPrivileges;
    /**
     * 操作人ip
     */
    @Column(length=50,name="operatorIP")
    private String operatorIP;
    /**
     * 操作类型
     */
    @Column(length=200,name="type")
    private String type;
    
    
    
    public SysLog() {
    }
    
    public SysLog(String type, String description) {
        this.operationTime = new Date();
        this.operator = SecurityUtil.getLoginUsername();
        if(this.operator == null){
            this.operator = "anonymous";
        }
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if(auth != null){
            StringBuilder sb = new StringBuilder();
            GrantedAuthority[] authorities = auth.getAuthorities();
            sb.append("{");
            for(GrantedAuthority a : authorities){
                sb.append(a.getAuthority());
                sb.append(",");
            }
            if(authorities.length > 0){
            	sb.deleteCharAt(sb.length() - 1);
            }
            sb.append("}");
            this.userPrivileges = sb.toString();
            if(this.userPrivileges.length() > 2000){
            	this.userPrivileges = this.userPrivileges.substring(0, 2000);
            }
        }
        this.operatorIP = SecurityUtil.getUserIp();
        
        if(description.length() > 100000){
        	this.zDescription = description.substring(0, 100000) + " ...";
        }else{
        	this.zDescription = description;
        }
        this.type = type;
    }
    


    public String getZDescription() {
		return zDescription;
	}

	public void setZDescription(String description) {
		zDescription = description;
	}

    public Date getOperationTime() {
        return operationTime;
    }
    public void setOperationTime(Date operationTime) {
        this.operationTime = operationTime;
    }
    public String getOperator() {
        return operator;
    }
    public void setOperator(String operator) {
        this.operator = operator;
    }
    public String getUserPrivileges() {
        return userPrivileges;
    }
    public void setUserPrivileges(String userPrivileges) {
        this.userPrivileges = userPrivileges;
    }
    public String getOperatorIP() {
        return operatorIP;
    }
    public void setOperatorIP(String operatorIP) {
        this.operatorIP = operatorIP;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
	
	
    public String toString(){
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        sb.append(this.operator);
        sb.append(",");
        sb.append(this.type);
        sb.append(",");
        sb.append(this.operatorIP);
        sb.append(",");
        sb.append(this.userPrivileges);
        sb.append(",");
        sb.append(this.operationTime);
        sb.append("]\r\n");
        sb.append(this.zDescription);
        return sb.toString();
    }
}
